#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMEssageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    m_websock = new QWebSocket();
    connect(m_websock,SIGNAL(connected()),this,SLOT(onConnexted()));
    connect(m_websock,SIGNAL(disconnected()),this,SLOT(onDisConnexted()));
    connect(m_websock,SIGNAL(pong(quint64,QByteArray)),this,SLOT(ClientHasPong(quint64,QByteArray)));
    connect(m_websock,SIGNAL(textFrameReceived(QString,bool)), this, SLOT(textFrameReceived(QString,bool)));
    connect(m_websock,SIGNAL(textMessageReceived(QString)), this, SLOT(textMessageReceived(QString)));
    connect(m_websock,SIGNAL(binaryFrameReceived(QByteArray,bool)), this, SLOT(binaryFrameReceived(QByteArray,bool)));
    connect(m_websock,SIGNAL(binaryMessageReceived(QByteArray)), this, SLOT(binaryMessageReceived(QByteArray)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::onConnexted()
{
    AppendLog("websocket connected.");
}


void MainWindow::onDisConnexted()
{
    AppendLog("websocket has disconnected.");
}

void MainWindow::textFrameReceived(QString a,bool b)
{

}

void MainWindow::textMessageReceived(QString a)
{
    QWebSocket *c = (QWebSocket*)sender();
    AppendLog( QString("[%1][TEXT][rx] %2").arg(c->peerAddress().toString()).arg(a));
}

void MainWindow::binaryFrameReceived(QByteArray a,bool b)
{
}

void MainWindow::binaryMessageReceived(QByteArray a)
{
    QWebSocket *c = (QWebSocket*)sender();
    AppendLog( QString("[%1][HEX][RX] %2").arg(c->peerAddress().toString()).arg(QString::fromLocal8Bit(a.toHex())));
}


void MainWindow::AppendLog(QString t)
{
    QString m= QDateTime::currentDateTime().toString("[HH:MM:SS.ZZZ]");
    ui->plainTextEdit->appendPlainText(  m + t );
}

void MainWindow::ViewSendText(QString t)
{
    QString m= QDateTime::currentDateTime().toString("[HH:MM:SS.ZZZ]");
    ui->plainTextEdit->appendPlainText( m + t );
}

void MainWindow::ViewRecvText(QString t)
{
    QString m= QDateTime::currentDateTime().toString("[HH:MM:SS.ZZZ]");
    ui->plainTextEdit->appendPlainText( m + t );
}

void MainWindow::on_pushButton_connect_clicked()
{
    m_websock->open(ui->lineEdit->text());
}

void MainWindow::on_pushButton_send_clicked()
{
    if( !m_websock->isValid() )
    {
        AppendLog("ws not connected.");
        return;
    }
    if( ui->comboBox->currentIndex() == 0 )
    {
        m_websock->sendTextMessage(ui->lineEdit_msg->text());
        ViewSendText(ui->lineEdit_msg->text());
    }
    else
    {
        QByteArray data = QByteArray::fromHex( ui->lineEdit_msg->text().toLocal8Bit() );
        m_websock->sendBinaryMessage(data);
        ViewSendText(ui->lineEdit_msg->text());
    }
}

void MainWindow::on_pushButton_ping_clicked()
{
    if( !m_websock->isValid() )
    {
        AppendLog("ws not connected.");
        return;
    }
    m_websock->ping(QByteArray::fromHex( ui->lineEdit_ping_msg->text().toLocal8Bit() ));
    ViewSendText("PING MSG[" + ui->lineEdit_ping_msg->text() + "]");
}
